//
//  SYFormCollectionView.m
//  SY_Merchant
//
//  Created by apple on 2019/1/5.
//  Copyright © 2019 zl_zmx. All rights reserved.
//

#import "SYFormCollectionView.h"

#import "SYFormCollectionViewLayout.h"
#import "SYFromCollectionViewCell.h"



@interface SYFormCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource,SYFormCollectionViewDelegateLayout>
{
    CGFloat _leftItemHeight;
    CGFloat _topItemWidth;
    UIView *_upperLeftCornerView;
    
    
}
/**      **/
@property(nonatomic, strong) UICollectionView *leftCollectionView;
/**      **/
@property(nonatomic, strong) UICollectionView *topCollectionView;
/**      **/
@property(nonatomic, strong) UICollectionView *contentCollectionView;




@end

@implementation SYFormCollectionView

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self.view addSubview:self.contentCollectionView];
    
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(SYUpperLeftCorner)]) {
        if ([self.dataSource respondsToSelector:@selector(SYLeftViewHeightFormCollectionView:)]) {
            _leftItemHeight = [self.dataSource SYLeftViewHeightFormCollectionView:self];
        }
        
        if ([self.dataSource respondsToSelector:@selector(SYTopViewWidthFormCollectionView:)]) {
            _topItemWidth = [self.dataSource SYTopViewWidthFormCollectionView:self];
        }
        
        CGSize leftCornerSzie = [self.dataSource SYUpperLeftCorner];
        self.contentCollectionView.contentInset = UIEdgeInsetsMake(leftCornerSzie.height, leftCornerSzie.width, 0, 0);
     
        
        
        NSUInteger numberItems = [self.dataSource SYFormCollectionView:self numberOfItemsInSection:0];
        NSUInteger numberOfSection = [self.dataSource SYNumberOfSectionsInSYFormCollectionView:self];
        
        
        [self.contentCollectionView addSubview:self.leftCollectionView];
        self.leftCollectionView.frame = CGRectMake(- leftCornerSzie.width, 0, leftCornerSzie.width, (_leftItemHeight + 1) * numberItems);
        
        [self.contentCollectionView addSubview:self.topCollectionView];
        self.topCollectionView.frame = CGRectMake(0, -leftCornerSzie.height,(_topItemWidth + 1) * numberOfSection + 13 , leftCornerSzie.height);
        
        
        
        _upperLeftCornerView = [self.dataSource SYCustomUpperLeftCornerView];
        _upperLeftCornerView.frame = CGRectMake( -leftCornerSzie.width, -leftCornerSzie.height, leftCornerSzie.width, leftCornerSzie.height);
        [self.contentCollectionView addSubview:_upperLeftCornerView];

    }
    
}

/**        */
static NSString* const SY_topCollectionView_cell_Id = @"SY_topCollectionView_cell_Id";
- (UICollectionView *)topCollectionView{
    if (!_topCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _topCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
        _topCollectionView.backgroundColor = UIColor.whiteColor;
        _topCollectionView.showsVerticalScrollIndicator = NO;
        _topCollectionView.showsHorizontalScrollIndicator = NO;
        _topCollectionView.delegate = self;
        _topCollectionView.dataSource = self;
        [_topCollectionView registerClass:[SYFromCollectionViewCell class] forCellWithReuseIdentifier:SY_topCollectionView_cell_Id];
    }
    
    return _topCollectionView;
}



- (UICollectionView *)leftCollectionView{
    if (!_leftCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _leftCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
        _leftCollectionView.backgroundColor = UIColor.whiteColor;
        _leftCollectionView.showsVerticalScrollIndicator = NO;
        _leftCollectionView.showsHorizontalScrollIndicator = NO;
        _leftCollectionView.delegate = self;
        _leftCollectionView.dataSource = self;
        [_leftCollectionView registerClass:[SYFromCollectionViewCell class] forCellWithReuseIdentifier:SY_topCollectionView_cell_Id];
    }
    
    return _leftCollectionView;
}


/**        */
static NSString* const SY_contentCollectionView_cell_Id = @"SY_contentCollectionView_cell_Id";
- (UICollectionView *)contentCollectionView{
    if (!_contentCollectionView) {
        SYFormCollectionViewLayout *layout = [[SYFormCollectionViewLayout alloc] init];
        layout.SYCollectionViewDelegateLayout = self;
        
        _contentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Size.width, kScreen_Size.height) collectionViewLayout:layout];
        _contentCollectionView.bounces = NO;
        _contentCollectionView.backgroundColor = [UIColor whiteColor];
        _contentCollectionView.delegate = self;
        _contentCollectionView.dataSource = self;
        [_contentCollectionView registerClass:[SYFromCollectionViewCell class] forCellWithReuseIdentifier:SY_contentCollectionView_cell_Id];
        
        
    }
    
    return _contentCollectionView;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    if (collectionView == self.contentCollectionView) {
        return [self.dataSource SYNumberOfSectionsInSYFormCollectionView:self];
    }
    
    return 1;
}

- (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.contentCollectionView || collectionView == self.leftCollectionView) {
        return [self.dataSource SYFormCollectionView:self numberOfItemsInSection:section];
    }
    
    if (collectionView == self.topCollectionView) {
        return [self.dataSource SYNumberOfSectionsInSYFormCollectionView:self];
    }
    return 0;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (collectionView == self.contentCollectionView) {
        SYFromCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:SY_contentCollectionView_cell_Id forIndexPath:indexPath];
        
        cell.backgroundColor = UIColor.lightGrayColor;
        
        cell.contentL.text = [NSString stringWithFormat:@"%ld=%ld  \n卡卡卡的",indexPath.section,indexPath.row];
        if (indexPath.section == 0) {
            if (indexPath.row == 3) {
                cell.contentL.hidden = YES;
            }
            else{
                cell.contentL.hidden =NO;
            }
        }
        else{
            cell.contentL.hidden = NO;
        }
        return cell;
    }
    else{
        
        SYFromCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:SY_topCollectionView_cell_Id forIndexPath:indexPath];
        
        cell.backgroundColor = UIColor.lightGrayColor;
        
        cell.contentL.text = [NSString stringWithFormat:@"%ld====%ld  \n你好左右卡的",indexPath.section,indexPath.row];
        
        cell.backgroundColor = UIColor.orangeColor;
        if (indexPath.section == 0) {
            if (indexPath.row == 3) {
                cell.contentL.hidden = YES;
            }
            else{
                cell.contentL.hidden =NO;
            }
        }
        else{
            cell.contentL.hidden = NO;
        }
        
        return cell;
    }

}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld----%ld ", indexPath.section, indexPath.row);
    
}





- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGSize LeftCornerSize = CGSizeMake(0, 0);
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(SYUpperLeftCorner)]) {
        LeftCornerSize = [self.dataSource SYUpperLeftCorner];
    }
    
    
    if (collectionView == self.topCollectionView) {
        return CGSizeMake(_topItemWidth ,LeftCornerSize.height);
    }
    
    if (collectionView == self.leftCollectionView) {
        return CGSizeMake(LeftCornerSize.width ,_leftItemHeight);
    }
    

    return CGSizeMake(0 ,0);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 1, 0, 1);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 1.0f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 1.0f;
}

#pragma mark === Cotnent 配置代理

- (CGSize)SYCollectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGSize size = CGSizeMake(0, 0);
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(SYContentViewHeightFormCollectionViewLeftView:heightForItemAtIndexPath:)]) {
        size = [self.dataSource SYContentViewHeightFormCollectionViewLeftView:self heightForItemAtIndexPath:indexPath];
    }
    
    
    return size;
}

- (UIEdgeInsets)SYCollectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 1, 0, 1);
}

- (CGFloat)SYCollectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 1.0f;
}

- (CGFloat)SYCollectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    
    return 1.0f;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetX = scrollView.contentOffset.x;
    CGFloat offsetY = scrollView.contentOffset.y + 20;
    
    CGRect cornerVieFrame = _upperLeftCornerView.frame;
    cornerVieFrame.origin.x = offsetX;
    cornerVieFrame.origin.y = offsetY;
    _upperLeftCornerView.frame = cornerVieFrame;
    
    
    CGRect leftFrame = self.leftCollectionView.frame;
    leftFrame.origin.x = offsetX;
    self.leftCollectionView.frame = leftFrame;
    
    CGRect topFrame = self.topCollectionView.frame;
    topFrame.origin.y = offsetY;
    self.topCollectionView.frame = topFrame;
    
    
}





- (void)formReloadData{
    [self.contentCollectionView reloadData];
    [self.leftCollectionView reloadData];
    [self.topCollectionView reloadData];
    
}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
